package com.baibei.ebec.user.ticket;

import android.content.Context;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.ITradeApi;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.model.Area;
import com.baibei.model.CouponResponseInfo;
import com.baibei.model.RankingInfo;
import com.baibei.sdk.ApiDefaultObserver;

import java.util.List;

import io.reactivex.functions.Action;

/**
 * 表现类实现
 * Created by shenminjie on 2017/9/1.
 * mail:shenminjie@100bei.com
 */

public class TicketPresenterImpl extends BasicPresenterImpl<TicketContract.TicketView> implements TicketContract.Presenter {

    private IUserApi mUserApi;
    private final ITradeApi mTradeApi;

    public TicketPresenterImpl(Context context, TicketContract.TicketView view) {
        super(context, view);
        mUserApi = ApiFactory.getInstance().getUserApi();
        mTradeApi = ApiFactory.getInstance().getTradeApi();
    }

    @Override
    public void start() {
        createObservable(mTradeApi.getRankingList(Area.ALL.name())).subscribe(new ApiDefaultObserver<List<RankingInfo>>() {
            @Override
            protected void onError(String s) {

            }

            @Override
            protected void accept(List<RankingInfo> rankingInfos) {
                mView.onLoadRankingInfoSuccess(rankingInfos);
            }
        });
        createObservable(mUserApi.queryCouponInfos())
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        mView.refreshCompleted();
                    }
                })
                .subscribe(new ApiDefaultObserver<CouponResponseInfo>() {
                    @Override
                    protected void onError(String s) {
                        mView.onLoadCouponFailed(s);
                    }

                    @Override
                    protected void accept(CouponResponseInfo couponResponseInfo) {
                        mView.onLoadCouponList(couponResponseInfo.getValid(), couponResponseInfo.getInvalid());
                    }
                });
    }
}
